import type { RecordType, UserTreePropEmits, UserTreeProps } from '../types/QueryCondition';
import { HeaderConfig } from '@minmetals/auth-lib';

import { computed, reactive, watch } from 'vue';

const commonUrl = `${import.meta.env.VITE_API_BASE_URL}${import.meta.env.VITE_API_BASE_FOUNDATION}`;
// const commonUrl = `${import.meta.env.VITE_API_BASE_FOUNDATION}`
export function UseUserTree(props: UserTreeProps, emits: UserTreePropEmits) {
  // const { t } = useI18n()
  const { title } = props;
  const dialogVisible = computed(() => props.dialogVisible);
  const tableData = computed(() => props.tableData);
  const selectedValue = computed(() => props.selectedValue || []);
  const userTreeBaseProps = reactive({
    params: { isTree: 1, orgCode: props.orgCode },
    loadUrl: `${commonUrl}/api/perm/role-user/load-org-user`,
    queryUrl: `${commonUrl}/api/perm/role-user/load-org-user/query`,
    // url: '/app-role-matrix/api/v1/extend-org/load-org-user',
    method: 'post',
    requestParams: {
      // baseURL: import.meta.env.VITE_API_BASE_FOUNDATION,
      headers: HeaderConfig.init().getHeaderConfig(),
    },
    multiple: true,
  });

  watch(() => (props.orgCode), () => {
    userTreeBaseProps.params = {
      isTree: 1,
      orgCode: props.orgCode,
    };
  }, { immediate: true });

  function closeDialog() {
    emits('closeDialog');
  }

  function submitDialog(data: RecordType[]) {
    emits('submitDialog', data);
  }

  return {
    dialogVisible,
    title,
    tableData,
    closeDialog,
    submitDialog,
    selectedValue,
    userTreeBaseProps,
  };
}
